<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="房屋编号" prop="houseId">
        <el-input
          v-model="queryParams.houseId"
          placeholder="请输入房屋编号"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="quitList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center"/>
      <el-table-column label="退租编号" align="center" prop="quitId"/>
      <el-table-column label="房屋编号" align="center" prop="houseId"/>
      <el-table-column label="申请人编号" align="center" prop="userId"/>
      <el-table-column label="退租日期" align="center" prop="quitTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.quitTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="状态" align="center" prop="status">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.house_is_handle" :value="scope.row.status"/>
        </template>
      </el-table-column>
      <el-table-column label="备注" align="center" prop="remark"/>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['domain:quit:edit']"
            v-if="scope.row.status == 0"
          >修改
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['domain:quit:remove']"
          >删除
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-document"
            @click="handleDetail(scope.row)"
            v-hasPermi="['domain:quit:query']"
          >查看详情
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-success"
            @click="handleQuit(scope.row)"
            v-hasPermi="['domain:quit:handle']"
            v-if="scope.row.status == 0"
          >处理退租
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 修改退租对象对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="550px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="120px">
        <el-form-item label="房屋编号" prop="houseId">
          <el-input v-model="form.houseId" disabled/>
        </el-form-item>
        <el-form-item label="申请人编号" prop="userId">
          <el-input v-model="form.userId" disabled/>
        </el-form-item>
        <el-form-item label="退租日期" prop="quitTime">
          <el-date-picker clearable size="small"
                          v-model="form.quitTime"
                          type="date"
                          value-format="yyyy-MM-dd"
                          :picker-options="pickerOptions"
                          placeholder="选择退租日期">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" type="textarea" placeholder="请输入内容"/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>


    <!-- 退租对象详情对话框 -->
    <el-dialog :title="title" :visible.sync="openDetail" width="600px" append-to-body>
      <el-form ref="form" :model="formData" label-width="120px">
        <el-form-item label="房屋编号" prop="houseId">
          <el-input v-model="formData.house.houseId" disabled/>
        </el-form-item>
        <el-form-item label="房屋名" prop="houseName">
          <el-input v-model="formData.house.houseName" disabled/>
        </el-form-item>
        <el-form-item label="申请人编号" prop="userId">
          <el-input v-model="formData.user.userId" disabled/>
        </el-form-item>
        <el-form-item label="申请人昵称" prop="nickName">
          <el-input v-model="formData.user.nickName" disabled/>
        </el-form-item>
        <el-form-item label="退租日期" prop="quitTime">
          <el-date-picker clearable size="small"
                          v-model="formData.quit.quitTime"
                          type="date"
                          value-format="yyyy-MM-dd"
                          disabled>
          </el-date-picker>
        </el-form-item>
        <el-form-item label="审批状态" prop="status">
          <el-radio-group v-model="formData.quit.status" disabled>
            <el-radio
              v-for="dict in dict.type.house_is_handle"
              :key="dict.value"
              :label="dict.value"
            >{{ dict.label }}
            </el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="formData.quit.remark" type="textarea" disabled/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {listQuit, getQuit, delQuit, updateQuit, quitDetail, handleQuitApp} from "@/api/system/quit";

export default {
  name: "Quit",
  dicts: ['house_is_handle'],
  data() {
    return {
      pickerOptions: {
        disabledDate: (time) => {
          return time.getTime() < Date.now() - 8.64e7;
        },
      },
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 退租对象表格数据
      quitList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      //是否显示详情层
      openDetail: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        houseId: null,
      },
      // 表单参数
      form: {},
      //详情表单参数
      formData: {
        user: {},
        house: {},
        quit: {}
      },
      // 表单校验
      rules: {
        quitTime: [
          {required: true, message: "退租时间不能为空!!!", trigger: "blur"},
        ]
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询退租对象列表 */
    getList() {
      this.loading = true;
      listQuit(this.queryParams).then(response => {
        this.quitList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.openDetail = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        quitId: null,
        houseId: null,
        userId: null,
        quitTime: null,
        createTime: null,
        updateTime: null,
        status: "0",
        remark: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.quitId)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const quitId = row.quitId || this.ids
      getQuit(quitId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改退租对象";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.quitId != null) {
            updateQuit(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const quitIds = row.quitId || this.ids;
      this.$modal.confirm('是否确认删除退租对象编号为"' + quitIds + '"的数据项？').then(function () {
        return delQuit(quitIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {
      });
    },
    //查看退租详情
    handleDetail(row) {
      quitDetail(row.quitId, row.houseId, row.userId).then(response => {
        this.formData.house = response.house;
        this.formData.user = response.user;
        this.formData.quit = response.quit;
        this.openDetail = true;
        this.title = "退租详情";
      })
    },
    //中介处理退租
    handleQuit(row) {
      const quitId = row.quitId;
      const houseId = row.houseId;
      this.$modal.confirm("是否处理该退租信息").then(function () {
        return handleQuitApp(quitId, houseId);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("处理成功");
      }).catch(() => {
      });

    }
  }
};
</script>
